package com.scalar.chiptrack.operations.dataentry.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.InTransitLots;
import com.scalar.chiptrack.operations.dataentry.dao.TransitDataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.forms.TransitLotDataEntryForm;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;

/**
 * Created by Sudhakar.
 * Date: Aug 30, 2005
 * Time: 6:03:36 PM
 */
public class TransitLotDataEntryAction extends ChipTrackAction
{

    private static final String ACTION_NAME = "/transitDataEntry.do";


    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {

        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME );
            return ( mapping.findForward( "login" ) );
        }

        HashMap errors  = new LinkedHashMap();
        HashMap info    = new LinkedHashMap();
        String action   = request.getParameter( "action" );
        String actionForward        = "success";
        ArrayList processTypeList       = null;
        ArrayList partNumberSelectList  = new ArrayList();
        ArrayList transitLocationList   = null;
        ArrayList nextLocationsList     = null;

        if ( action == null )
        {
            action = "";
        }

        TransitLotDataEntryForm transitDataEntryForm = ( TransitLotDataEntryForm ) form;
        //TransitDataEntryDAO transitDataEntryDAO = new TransitDataEntryDAO();

        request.setAttribute( "action", action );
        processTypeList = getProcessList();
        request.setAttribute( "processTypeList", processTypeList );

        String processStepId = transitDataEntryForm.getProcessStepId();
        String lotId = transitDataEntryForm.getShippedLotid();
        String processType = transitDataEntryForm.getProcessType();
        if ( processType != null )
        {
            nextLocationsList = getNextLocation( processType );
            request.setAttribute( "nextLocationsList", nextLocationsList );
        }

        if ( action.equalsIgnoreCase( "processType" ) || action.equalsIgnoreCase( "partnumber" ) || action.equalsIgnoreCase( "location" ) || action.equalsIgnoreCase( "save" ) || action.equalsIgnoreCase( "lotnumber" ) )
        {
            partNumberSelectList = getProductList( processType );

            request.setAttribute( "partNumberSelectList", partNumberSelectList );

            if ( processStepId != null && processStepId.trim().length() > 0 )
            {
                transitLocationList = getLocationList( processStepId );
                request.setAttribute( "transitLocationList", transitLocationList );
            }
        }
        if ( action.equalsIgnoreCase( "location" ) || action.equalsIgnoreCase( "lotnumber" ) || action.equalsIgnoreCase( "save" ) )
        {
            ArrayList lotNumbersList = null;
            lotNumbersList = getLotList( processStepId );
            request.setAttribute( "lotNumbersList", lotNumbersList );
            if ( action.equals( "location" ) )
            {
                transitDataEntryForm.setShippedLotid( "0" );
            }
        }
        if ( action.equalsIgnoreCase( "lotnumber" ) )
        {
            if ( ( !"-1".equalsIgnoreCase( lotId ) ) )
            {
                InTransitLots inTransitLots = TransitDataEntryDAO.loadAllTransitLotInfo( lotId );

                transitDataEntryForm.setShipDate( inTransitLots.getShipDate() );
                transitDataEntryForm.setShipQty( inTransitLots.getShipQty() );
                transitDataEntryForm.setNextLocation( inTransitLots.getNextLocation() );
                transitDataEntryForm.setCustomerPO( inTransitLots.getCustomerPO() );
                transitDataEntryForm.setCustomerSO( inTransitLots.getCustomerSO() );
                transitDataEntryForm.setEtd( inTransitLots.getEtd() );
                transitDataEntryForm.setEta( inTransitLots.getEta() );
                transitDataEntryForm.setWaferId( inTransitLots.getWaferId() );
                transitDataEntryForm.setInvoiceNO( inTransitLots.getInvoiceNo() );
                transitDataEntryForm.setComments( inTransitLots.getComments() );
                transitDataEntryForm.setLotNumber( inTransitLots.getLotNumber() );

            }
            else
            {
                transitDataEntryForm.setShipDate( "" );
                transitDataEntryForm.setShipQty( "" );
                transitDataEntryForm.setNextLocation( "" );
                transitDataEntryForm.setCustomerPO( "" );
                transitDataEntryForm.setCustomerSO( "" );
                transitDataEntryForm.setEtd( "" );
                transitDataEntryForm.setEta( "" );
                transitDataEntryForm.setWaferId( "" );
                transitDataEntryForm.setInvoiceNO( "" );
                transitDataEntryForm.setComments( "" );
                transitDataEntryForm.setShipDate( "" );
                transitDataEntryForm.setShipDate( "" );
                transitDataEntryForm.setLotNumber( "" );

            }

        }
        if ( action.equalsIgnoreCase( "save" ) )
        {
            String process = null;
            int blankFGICount = TransitDataEntryDAO.checkBlankFGILot( transitDataEntryForm.getLotNumber(), transitDataEntryForm.getProcessStepId() );

            if( blankFGICount > 0 )
            {
                 if( transitDataEntryForm.getProcessType().equalsIgnoreCase( "FAB") )
                 {
                     process = "Wafer Store";
                 }
                 else
                 {
                     process = "Blank FGI";
                 }

                 errors.put( transitDataEntryForm.getLotNumber() + "  already moved to  " , process );
                 request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            }
            else
            {
                InTransitLots transitLotInfo = new InTransitLots();

                transitLotInfo.setProcessStepId( transitDataEntryForm.getProcessStepId() );
                transitLotInfo.setLotNumber( transitDataEntryForm.getLotNumber() );
                transitLotInfo.setShipQty( transitDataEntryForm.getShipQty() );
                transitLotInfo.setShipDate( transitDataEntryForm.getShipDate() );
                transitLotInfo.setInvoiceNo( transitDataEntryForm.getInvoiceNO() );
                transitLotInfo.setCustomerPO( transitDataEntryForm.getCustomerPO() );
                transitLotInfo.setCustomerSO( transitDataEntryForm.getCustomerSO() );
                transitLotInfo.setEta( transitDataEntryForm.getEta() );
                transitLotInfo.setEtd( transitDataEntryForm.getEtd() );
                transitLotInfo.setComments( transitDataEntryForm.getComments() );
                transitLotInfo.setProcessType( transitDataEntryForm.getProcessType() );
                transitLotInfo.setLocationId( transitDataEntryForm.getLocationId() );
                transitLotInfo.setNextLocation( transitDataEntryForm.getNextLocation() );
                transitLotInfo.setWaferId( transitDataEntryForm.getWaferId() );
                transitLotInfo.setShippedLotID( transitDataEntryForm.getShippedLotid() );

                try
                {
                	TransitDataEntryDAO.saveTransitLot( null, transitLotInfo );
                    actionForward = "saveSuccess";
                    info.put( "Data has been updated successfully", "" );
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    errors.put( "Error storing the Transit Lot information", null );
                }
            }
        }
        request.removeAttribute( "transitDataEntryForm" );
        if ( action.equals( "null" ) )
        {
            transitDataEntryForm.setProcessType( "0" );
            request.setAttribute( SessionKeys.INFO_MESSAGES, request.getAttribute( SessionKeys.INFO_MESSAGES ) );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, request.getAttribute( SessionKeys.ERROR_MESSAGES ) );
        }
        else
        {
            request.setAttribute( SessionKeys.INFO_MESSAGES, info );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        }
        request.setAttribute( "transitDataEntryForm", transitDataEntryForm );

        return ( mapping.findForward( actionForward ) );

    }
    /**
     *
     * @return
     */
    public ArrayList getProcessList()
    {
        ArrayList processList = new ArrayList();
        processList.add( new org.apache.struts.util.LabelValueBean( "FAB", "FAB" ) );
        processList.add( new org.apache.struts.util.LabelValueBean( "TEST", "TEST" ) );
        return processList;
    }
    /**
     *
     * @param process
     * @return
     * @throws ScalarException
     */
    public ArrayList getProductList( String process ) throws ScalarException
    {
        ArrayList partNumberSelectList = new ArrayList();
        ArrayList partNumberList = null;
        PartNumber partNumber = null;

        if ( ( "FAB".equals( process ) ) ||
                ( "TEST".equals( process ) ) )
        {
            partNumberList = ProductDao.getPartNumber( process );
            for ( int i = 0; i < partNumberList.size(); i++ )
            {
                partNumber = ( PartNumber ) partNumberList.get( i );
                partNumberSelectList.add( new org.apache.struts.util.LabelValueBean( partNumber.getPartNumber(), partNumber.getProcessStepId() ) );
            }
        }

        return partNumberSelectList;
    }

    /**
     * Returns the list of locations of this process step
     *
     * @param processStepId
     * @return
     */
    public ArrayList getLocationList( String processStepId ) throws ScalarException
    {
        ArrayList locationList = null;
        ArrayList locationSelectList = new ArrayList();
        VendorLocation vendorLocation = null;

        if ( processStepId != null )
        {
            locationList = VendorDAO.getSelectedLocations( Integer.parseInt( processStepId ) );
        }


        for ( int i = 0; i < locationList.size(); i++ )
        {
            vendorLocation = ( VendorLocation ) locationList.get( i );
            locationSelectList.add( new org.apache.struts.util.LabelValueBean( vendorLocation.getLocationName(), vendorLocation.getLocationID() ) );
        }

        return locationSelectList;
    }

    /**
     * Returns the list of lots matching the given processStepId and locationId and process type
     *    * @param processStepId
     * @return
     */
    private ArrayList getLotList( String processStepId ) throws ScalarException
    {
        ArrayList lotList = new ArrayList();
        InTransitLots lot = null;

        ArrayList lotNumberList = TransitDataEntryDAO.loadAllTransitLots( processStepId );
        lotList = new ArrayList();


        for ( int i = 0; i < lotNumberList.size(); i++ )
        {
            lot = ( InTransitLots ) lotNumberList.get( i );

            lotList.add( new org.apache.struts.util.LabelValueBean( lot.getLotNumber(), lot.getShippedLotID() ) );


        }
        return lotList;
    }

    /**
     *
     * @param processType
     * @return
     */
    private ArrayList getNextLocation( String processType ) throws Exception
    {

        ArrayList nextLocationList = new ArrayList();

        if( processType.equalsIgnoreCase("FAB"))
        {

          nextLocationList.add( new org.apache.struts.util.LabelValueBean( "AMK KOREA", "AMK KOREA" ) );
          nextLocationList.add( new org.apache.struts.util.LabelValueBean( "ITEST", "ITEST" ) );
          nextLocationList.add( new org.apache.struts.util.LabelValueBean( "MATRIX-Prod", "MATRIX-Prod" ) );

        }
        else
        {
            nextLocationList = TransitDataEntryDAO.loadClassStroeLocations( null );

        }

        return  nextLocationList;
    }
}
